package com.topisv.tms.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.SubWarehouseRel;
import com.topisv.tms.entity.stock.instorage.InstorageBill;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.SubWarehouseRelService;
import com.topisv.tms.service.stock.instorage.InstorageBillService;
import com.topisv.tms.web.base.BaseDataTableController;

import net.sf.json.JSONObject;
/**
 * 服务网点管理的Controller, 使用Restful风格的Urls:
 * 
 * List page     : GET /areaGroup/
 * Create page   : GET /areaGroup/create
 * Create action : POST /areaGroup/create
 * Update page   : GET /areaGroup/update/{id}
 * Update action : POST /areaGroup/update
 * Delete action : GET /areaGroup/delete/{id}
 * 
 * @author Arthur
 */
@Controller
@RequestMapping(value = "/subWarehouseRel")
public class SubWarehouseRelController extends BaseDataTableController{
	private static final Logger logger = LoggerFactory.getLogger(SubWarehouseRelController.class);

	@Autowired
	private AreaGroupService areaGroupService;
	
	@Autowired
	private SubWarehouseRelService subWarehouseRelService;
	
	@Autowired
	private InstorageBillService instorageBillService;
	/**
	 * 查询分仓
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "querySubWarehouse",method=RequestMethod.POST)
	public void querySubWarehouse(@RequestParam String billId,Model model,HttpServletResponse response) {
		JSONObject json=null;
		PrintWriter out=null;
		try {
			out=response.getWriter();
			json=new JSONObject();
			
			InstorageBill instorageBill = instorageBillService.loadBillById(Long.valueOf(billId));
			if(instorageBill==null){
				json.put("status", "fail");
				out.print(json.toString());
				return;
			}
			//获取子仓
			List<SubWarehouseRel> swrList = subWarehouseRelService.findByMainWarehouseCode(instorageBill.getInwarehouseCode());
			
			fillSelectValue(model, instorageBill.getCustomerCode());
			
			List<AreaGroup> displayWarehouseList = new ArrayList<AreaGroup>();
			List<AreaGroup>  warehouseList = (List<AreaGroup>)model.asMap().get("warehouseList");
			for(AreaGroup areaGroup:warehouseList) {
				boolean result = false;
				if(areaGroup.getGroupCode().equals(instorageBill.getInwarehouseCode())){
					result = true;
				}else {
					for(SubWarehouseRel r:swrList){
						if(r.getSubWarehouseCode()!=null && r.getSubWarehouseCode().equals(areaGroup.getGroupCode())){
							result = true;
							break;
						}
					}
				}
				if(result) {
					displayWarehouseList.add(areaGroup);
				}
			}
			json.put("warehouseList", displayWarehouseList);
			json.put("status", "success");
			out.print(json.toString());
			log.debug("分配主仓编码:"+json.toString());
		} catch (Exception e) {
			log.error("error",e);
		}
	}
	
}
